#!/usr/bin/env julia

"""
Basic Solver Testing Script
Tests the advanced solvers in GSICoreAnalysis package
"""

using GSICoreAnalysis
using GSICoreAnalysis.AdvancedSolvers
using LinearAlgebra
using Random

println("=== GSI Core Analysis Advanced Solvers Test ===")

# Create test problem
n = 10
Random.seed!(1234)
A = rand(n, n)
A = A + A' + n * I  # Make symmetric positive definite
b = rand(n)
x_true = A \ b

println("\nTest Problem:")
println("Matrix size: $(size(A))")
println("Condition number: $(cond(A))")
println("True solution norm: $(norm(x_true))")

# Test 1: Lanczos Solver Configuration
println("\n=== Test 1: Lanczos Solver ===")
try
    lanczos_config = LanczosConfig(
        max_iter = 50,
        tolerance = 1e-10,
        verbose = true
    )
    
    println("✅ Lanczos configuration created successfully")
    println("  Max iterations: $(lanczos_config.max_iter)")
    println("  Tolerance: $(lanczos_config.tolerance)")
    println("  Precondition: $(lanczos_config.precondition)")
    
catch e
    println("❌ Lanczos configuration failed: $e")
end

# Test 2: BiCG-Lanczos Solver Configuration  
println("\n=== Test 2: BiCG-Lanczos Solver ===")
try
    # Use default constructor
    bicg_config = BiCGLanczosConfig{Float64}(
        100,     # max_iterations
        1e-8,    # tolerance
        0,       # restart_frequency
        BiCG_STANDARD,  # variant
        false,   # look_ahead
        false,   # stabilization
        true,    # precondition
        false,   # verbose
        10,      # precond_steps
        1e-2     # breakdown_tolerance
    )
    
    println("✅ BiCG-Lanczos configuration created successfully")
    println("  Max iterations: $(bicg_config.max_iterations)")
    println("  Variant: $(bicg_config.variant)")
    
catch e
    println("❌ BiCG-Lanczos configuration failed: $e")
end

# Test 3: QuasiNewton Solver Configuration
println("\n=== Test 3: QuasiNewton Solver ===")
try
    # Use default constructor parameters
    qn_config = QuasiNewtonConfig{Float64}(
        100,     # max_iterations
        1e-8,    # tolerance
        1e-6,    # gradient_tolerance
        1e-12,   # step_tolerance
        5,       # memory_length
        ARMIJO,  # line_search
        NO_TRUST_REGION,  # trust_region
        1.0,     # initial_step
        0.1,     # step_reduction
        2.0,     # step_increase
        false    # verbose
    )
    
    println("✅ QuasiNewton configuration created successfully")
    println("  Max iterations: $(qn_config.max_iterations)")
    println("  Memory length: $(qn_config.memory_length)")
    println("  Line search: $(qn_config.line_search)")
    
catch e
    println("❌ QuasiNewton configuration failed: $e")
end

# Test 4: Basic solver state objects
println("\n=== Test 4: Solver State Objects ===")
try
    # Test LanczosState
    lanczos_state = LanczosState{Float64}(
        n,              # n
        zeros(n),       # residual
        zeros(n),       # v_current
        zeros(n),       # v_previous
        zeros(n),       # w
        zeros(10, 10),  # T_matrix
        zeros(10),      # alpha
        zeros(10),      # beta
        1,              # iteration
        1.0,            # residual_norm
        false,          # converged
        zeros(10)       # ritz_estimates
    )
    
    println("✅ LanczosState created successfully")
    println("  Problem size: $(lanczos_state.n)")
    
    # Test BiCGLanczosState  
    bicg_state = BiCGLanczosState{Float64}(
        n,           # n
        zeros(n),    # residual
        zeros(n),    # p_vector
        zeros(n),    # v_vector
        zeros(n),    # s_vector
        zeros(n),    # t_vector
        1,           # iteration
        1.0,         # residual_norm
        false        # converged
    )
    
    println("✅ BiCGLanczosState created successfully")
    println("  Problem size: $(bicg_state.n)")
    
    # Test QuasiNewtonState
    qn_state = QuasiNewtonState{Float64}(
        n,                # n
        zeros(n),         # x_current
        zeros(n),         # gradient_current
        zeros(n),         # direction
        zeros(n, 5),      # s_vectors
        zeros(n, 5),      # y_vectors
        zeros(5),         # rho_values
        1,                # iteration
        0,                # memory_used
        1.0,              # gradient_norm
        false             # converged
    )
    
    println("✅ QuasiNewtonState created successfully")
    println("  Problem size: $(qn_state.n)")
    println("  Memory capacity: $(size(qn_state.s_vectors, 2))")
    
catch e
    println("❌ State object creation failed: $e")
end

println("\n=== Summary ===")
println("✅ GSICoreAnalysis package loaded successfully")
println("✅ AdvancedSolvers module accessible")
println("✅ Basic solver configurations working")
println("✅ State objects can be created")

println("\n🎉 Basic solver functionality test completed!")
println("\nNext steps:")
println("- Test actual solver algorithms")
println("- Test convergence on various problems")
println("- Performance benchmarking")